package cn.iocoder.yudao.module.resource.controller.admin.audiotext;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.resource.controller.admin.audiotext.vo.AudioTextConvertAudioRespVO;
import cn.iocoder.yudao.module.resource.controller.admin.audiotext.vo.AudioTextConvertAudioVO;
import cn.iocoder.yudao.module.resource.controller.admin.audiotext.vo.AudioTextPageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.audiotext.vo.AudioTextRespVO;
import cn.iocoder.yudao.module.resource.controller.admin.audiotext.vo.AudioTextSaveReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.audiotext.vo.RoleTextAudioPageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.audiotext.vo.RoleTextAudioRespVO;
import cn.iocoder.yudao.module.resource.dal.dataobject.audiotext.AudioTextDO;
import cn.iocoder.yudao.module.resource.service.audiotext.AudioTextService;
import cn.iocoder.yudao.module.resource.service.roletextaudio.RoleTextAudioService;
import cn.iocoder.yudao.module.resource.service.roletextaudio.TextConvertRoleAudioService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 文本编辑")
@RestController
@RequestMapping("/audiotext/audio-text")
@Validated
public class AudioTextController {

    @Resource
    private AudioTextService audioTextService;
    @Resource
    private RoleTextAudioService roleTextAudioService;
    @Resource
    private TextConvertRoleAudioService textConvertRoleAudioService;

    @PostMapping("/create")
    @Operation(summary = "创建文本编辑")
    @PreAuthorize("@ss.hasPermission('audiotext:audio-text:create')")
    public CommonResult<Integer> createAudioText(@Valid @RequestBody AudioTextSaveReqVO createReqVO) {
        return success(audioTextService.createAudioText(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文本编辑")
    @PreAuthorize("@ss.hasPermission('audiotext:audio-text:update')")
    public CommonResult<Boolean> updateAudioText(@Valid @RequestBody AudioTextSaveReqVO updateReqVO) {
        audioTextService.updateAudioText(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文本编辑")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('audiotext:audio-text:delete')")
    public CommonResult<Boolean> deleteAudioText(@RequestParam("id") Integer id) {
        audioTextService.deleteAudioText(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文本编辑")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('audiotext:audio-text:query')")
    public CommonResult<AudioTextRespVO> getAudioText(@RequestParam("id") Integer id) {
        AudioTextRespVO audioText = audioTextService.getAudioText(id);
        return success(audioText);
    }

    @GetMapping("/page")
    @Operation(summary = "获得文本编辑分页")
    @PreAuthorize("@ss.hasPermission('audiotext:audio-text:query')")
    public CommonResult<PageResult<AudioTextRespVO>> getAudioTextPage(@Valid AudioTextPageReqVO pageReqVO) {
        PageResult<AudioTextDO> pageResult = audioTextService.getAudioTextPage(pageReqVO);
        List<AudioTextRespVO> audioTextRespVOList = new ArrayList<>();
        pageResult.getList().forEach(c -> {
            AudioTextRespVO audioTextRespVo = audioTextService.createAudioTextRespVo(c);
            audioTextRespVOList.add(audioTextRespVo);
        });
        PageResult<AudioTextRespVO> voPageResult = new PageResult<>(audioTextRespVOList, pageResult.getTotal());
        return success(voPageResult);
    }

    @PostMapping("/convertAudio")
    @Operation(summary = "文本转换音频")
    @PreAuthorize("@ss.hasPermission('audiotext:audio-text:create')")
    public CommonResult<AudioTextConvertAudioRespVO> convertAudio(@Valid @RequestBody AudioTextConvertAudioVO param) {
        roleTextAudioService.saveTextRoleAudio(param);
        textConvertRoleAudioService.convertAudio();
        return CommonResult.success(new AudioTextConvertAudioRespVO());
    }

    @PostMapping("/getRoleTextAudioRecord")
    @Operation(summary = "获取角色文本转音频的记录")
    @PreAuthorize("@ss.hasPermission('audiotext:audio-text:create')")
    public CommonResult<PageResult<RoleTextAudioRespVO>> getRoleTextAudioRecord(@RequestBody RoleTextAudioPageReqVO pageReqVO) {
        PageResult<RoleTextAudioRespVO> roleTextAudioPage = roleTextAudioService.getRoleTextAudioPage(pageReqVO);
        return CommonResult.success(roleTextAudioPage);
    }
}